<?php
class BooksController extends AppController {

	var $name = 'Books';
	
	var $paginate = array('Product'=>array('limit'=>20));

	function index() {		
		$books  = $this->Book->find('all',array('order'=>array('Product.score DESC') ));
		$this->set('books',$books);
		$user = $this->Session->read('Auth.User');
	}
	function adminindex(){
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Books->recursive = 0;
			$this->set('books', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid book', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->loadModel('Product');
		$this->loadModel('Comment');
		$book = $this->Book->read(null, $id);
		$book += $this->Product->read(null,$id);
		$comment = $this->Comment->find('all',array('conditions'=>array('product_id'=>$book['Product']['id']),'fields'=>array('User.username','User.id','Comment.*')));
		$book['Comment'] = $comment;
		$genre = $book['Product']['genre'];
		//$this->Book->contain(array('Book','Book.Producer'));
        //$related_book = "select * from products as Product,producers as Producer,books as Book where $genre = Product.genre order by Product.sold_count desc limit 3";
        $related_book = $this->Book->find("all",array('conditions'=>array('Product.genre'=>$genre,'Book.id <>'=>$book['Product']['id']),'order'=>'Product.sold_count desc','limit' => 3));
	    $other_books = $this->Book->find("all",array('conditions'=>array('Book.producer_id'=>$book['Book']['producer_id'],'Book.id <>'=>$book['Product']['id']),'order'=>'Product.sold_count desc','limit' => 3));
        $this->set(compact('book','related_book','other_books'));
	  
	}
	function adminview($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid book', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->loadModel('Product');
			$this->loadModel('Comment');
			$book = $this->Book->read(null, $id);
			$book += $this->Product->read(null,$id);
			$comment = $this->Comment->find('all',array('conditions'=>array('product_id'=>$book['Product']['id']),'fields'=>array('User.username','User.id','Comment.*')));
			$book['Comment'] = $comment;
			$user = $this->Session->read('Auth.User');
		    $this->set(compact('book','user'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!empty($this->data)) {
				$this->setDefault($this->data);
				$this->Product->create();
				pr($this->data);
				if ($this->Product->save($this->data)) {				 
					$this->data['Book']['id'] = $this->Product->id;
					pr($this->data);
					$this->Book->create();
					if ($this->Book->save($this->data)) {
						$this->Session->setFlash(__('The book has been saved', true));
						//$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The book could not be saved. Please, try again.', true));
					}
				} else {
					$this->Session->setFlash(__('The book could not be saved. Please, try again.', true));
				}
			}
			$producers = $this->Book->Producer->find('list');
			$this->set(compact('producers'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid book', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				$this->setDefault($this->data);
				if ($this->Product->save($this->data)) {
					$this->data['Book']['id'] = $this->Product->id;
					$this->Book->create();
					if ($this->Book->save($this->data)) {
						$this->Session->setFlash(__('The book has been saved', true));
						$this->redirect(array('action' => 'adminindex'));
					} else {
						$this->Session->setFlash(__('The book could not be saved. Please, try again.', true));
					}
				} else {
					$this->Session->setFlash(__('The book could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Book->read(null, $id);
				$this->data += $this->Product->read(null, $id);
			}
			$producers = $this->Book->Producer->find('list');
			$this->set(compact('producers'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->loadModel('Product');
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for book', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Book->delete($id)) {
				if ($this->Product->delete($id)) {
					$this->Session->setFlash(__('Book deleted', true));
					$this->redirect(array('action'=>'adminindex'));
				}
			}
			$this->Session->setFlash(__('Book was not deleted', true));
			$this->redirect(array('action' => 'adminindex'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function setDefault(&$data) {
		if($data['Product']['image']=='') {
			$data['Product']['image']='urun-resmi-yok.png';
		}
		
		if($data['Product']['sold_count']=='') {
			$data['Product']['sold_count']='0';
		}
		
		if($data['Product']['stock_count']=='') {
			$data['Product']['stock_count']='0';
		}
	}
	
	function category($genre) {
		// $books=$this->Book->query("select * from books where ID=2");
		$books  = $this->Book->find('all',array('conditions'=>array('Product.genre'=>$genre),'order'=>array('Product.score DESC') ));
		//$books  = $this->Book->find('first',array('conditions'=>array('Book.id'=>'1'),'fields'=>array('Book.id','Product.name')));
		//$books  = $this->Book->find('first' ,array('conditions'=>array('Book.id'=>'1'),'fields'=>array('Book.id','Product.name')));
		//$books = $this->Book->read(array('Product.name','Book.id'),'1');
		
		$this->set('books',$books);
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
